package com.epk.pic.controller;

import com.epk.pic.controller.base.BaseController;
import com.epk.pic.mapper.PicMapper;
import com.epk.pic.mapper.TargetMapper;
import com.epk.pic.po.PicContents;
import com.epk.pic.po.PicInfoMain;
import com.epk.pic.po.PicTargetMain;
import com.epk.pic.po.PicTargets;
import com.epk.pic.service.PicService;
import com.epk.pic.vo.PicInfoVo;
import com.epk.pic.vo.PicQueryVo;
import com.epk.pic.vo.base.ApiResponse;
import com.epk.pic.vo.base.TableDataInfo;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/tag")
public class TargetController extends BaseController {

    @Autowired
    private TargetMapper targetMapper;

    @GetMapping("/path/{tag}")
    public ApiResponse<String> targetPath(@PathVariable String tag) {
        return ApiResponse.succ(targetMapper.selectTargetPath(999, tag));
    }

    @PostMapping("/sub/{tag}")
    public ApiResponse<List<PicTargetMain>> targets(@PathVariable String tag) {
        return ApiResponse.succ(targetMapper.selectTargetAndSub(999, tag));
    }

    @PostMapping("/addTag")
    public ApiResponse<Void> addTag(@PathParam("tagName") String tagName, @PathParam("tagVal") String tagVal, @PathParam("item") Long item) {
        PicTargets picTargets = new PicTargets();
        picTargets.setTargetName(tagName);
        picTargets.setTargetValue(tagVal);
        picTargets.setPicMainID(item);
        targetMapper.savePicTargets(picTargets);
        return ApiResponse.succ();
    }

    @PostMapping("/delTag")
    public ApiResponse<Void> delTag(@PathParam("tagName") String tagName, @PathParam("tagVal") String tagVal, @PathParam("item") Long item) {
        PicTargets picTargets = new PicTargets();
        picTargets.setTargetName(tagName);
        picTargets.setTargetValue(tagVal);
        picTargets.setPicMainID(item);
        targetMapper.deleteTargetByMaimIdAndTag(picTargets);
        return ApiResponse.succ();
    }

    @PostMapping("/addGroup")
    public ApiResponse<Void> addGroup(@RequestBody PicTargetMain tagMain) {
        if (!PicService.getAdminPwd().equals(tagMain.getPassword())) {
            return ApiResponse.err("管理员密码未匹配！");
        }
        tagMain.setId(null);
        tagMain.setParentTarget(nvl(tagMain.getParentTarget(), "other"));
        tagMain.setTargetCount(0);
        tagMain.setTargetLevel(nvl(tagMain.getTargetLevel(), 3));
        targetMapper.savePicTargetMain(tagMain);
        return ApiResponse.succ();
    }

    private <T> T nvl(T from, T to) {
        if (from == null || "".equals(from.toString())) {
            return to;
        }
        return from;
    }
}